package org.dev.biz_order.address

import android.text.TextUtils
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import org.dev.`as`.project.common.http.ApiFactory
import org.dev.`as`.project.common.ui.view.showToast
import org.dev.hi.library.restful.HiCall
import org.dev.hi.library.restful.HiCallback
import org.dev.hi.library.restful.HiResponse

/**
 * Author WenPing
 * CreateTime 2021/9/4.
 * Description:地址的viewModel
 */
class AddressViewModel : ViewModel() {

    var checkedPosition: Int = -1
    var checkedAddressItem: AddressItem? = null

    /**
     * 查询地址列表
     */
    fun queryAddressList(): LiveData<List<Address>?> {
        val liveData = MutableLiveData<List<Address>?>()
        ApiFactory
            .create(AddressApi::class.java)
            .queryAddress(1, 100)
            .enqueue(object : HiCallback<AddressModel> {
                override fun onSuccess(hiResponse: HiResponse<AddressModel>) {
                    liveData.postValue(hiResponse.data?.list)
                }

                override fun onFailed(throws: Throwable) {
                    liveData.postValue(null)
                }
            })
        return liveData
    }

    /**
     * 保存地址
     */
    fun saveAddress(
        province: String,
        city: String,
        area: String,
        detail: String,
        receiver: String,
        phone: String
    ): LiveData<Address?> {
        val liveData = MutableLiveData<Address?>()
        ApiFactory.create(AddressApi::class.java)
            .addAddress(province, city, area, detail, receiver, phone)
            .enqueue(object : HiCallback<String> {
                override fun onSuccess(hiResponse: HiResponse<String>) {
                    if (hiResponse.successful()) {
                        val address = Address(province, city, area, detail, receiver, phone, "", "")
                        liveData.postValue(address)
                    } else {
                        if (!TextUtils.isEmpty(hiResponse.msg)) showToast(hiResponse.msg!!)
                    }
                }

                override fun onFailed(throws: Throwable) {
                    if (!TextUtils.isEmpty(throws.message)) showToast(throws.message!!)
                }
            })
        return liveData
    }

    /**
     * 更新地址
     */
    fun updateAddress(
        id: String,
        province: String,
        city: String,
        area: String,
        detail: String,
        receiver: String,
        phone: String
    ): LiveData<Address?> {
        val liveData = MutableLiveData<Address?>()
        ApiFactory.create(AddressApi::class.java)
            .updateAddress(id, province, city, area, detail, receiver, phone)
            .enqueue(object : HiCallback<String> {
                override fun onSuccess(hiResponse: HiResponse<String>) {
                    if (hiResponse.successful()) {
                        val address = Address(province, city, area, detail, receiver, phone, "", "")
                        liveData.postValue(address)
                    } else {
                        if (!TextUtils.isEmpty(hiResponse.msg)) showToast(hiResponse.msg!!)
                    }
                }

                override fun onFailed(throws: Throwable) {
                    if (!TextUtils.isEmpty(throws.message)) showToast(throws.message!!)
                }
            })
        return liveData
    }

    fun deleteAddress(addressId: String): LiveData<Boolean> {
        val liveData = MutableLiveData<Boolean>()
        ApiFactory.create(AddressApi::class.java)
            .deleteAddress(addressId)
            .enqueue(object : HiCallback<String> {
                override fun onSuccess(hiResponse: HiResponse<String>) {
                    if (hiResponse.successful()) {
                        liveData.postValue(hiResponse.successful())
                    } else {
                        showToast("地址删除失败")
                    }
                }

                override fun onFailed(throws: Throwable) {
                    showToast("地址删除失败:" + throws.message)
                }
            })
        return liveData
    }

    /**
     * This method will be called when this ViewModel is no longer used and will be destroyed
     * viewModel 被销毁的时候重置
     */
    override fun onCleared() {
        checkedAddressItem = null
        checkedPosition = -1
        super.onCleared()
    }

}